package com.come4loves.game.module.battle.handler;

import io.netty.channel.ChannelHandlerContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.come4loves.core.comunicate.protocol.Response;
import com.come4loves.core.comunicate.router.annotation.Cmd;
import com.come4loves.core.result.Result;
import com.come4loves.game.module.battle.service.BattleService;
import com.come4loves.game.proto.BattleCmd;
import com.come4loves.game.proto.BattleProto;
import com.come4loves.game.proto.BattleProto.BattleStartRequest.BattleType;
import com.come4loves.game.proto.BattleProto.UseCardData;
import com.come4loves.game.proto.ModuleName;
import com.come4loves.game.server.router.GatewayRouterHandlerImpl;
import com.come4loves.game.server.session.PlayerContext;
@Component
public class BattleHandler extends GatewayRouterHandlerImpl {

	@Autowired
	private PlayerContext playerContext;
	@Autowired
	private BattleService battleService;
	@Override
	public byte getModule() {
		return ModuleName.Module.BATTLE_VALUE;
	}
	
	@Cmd(id = BattleCmd.Battle.BATTLE_START_VALUE)
	public Result battleStart(ChannelHandlerContext context,byte[] bytes, Response response) throws Exception {
		long actorId = playerContext.getActorId(context);
		BattleProto.BattleStartRequest request = BattleProto.BattleStartRequest.parseFrom(bytes);
		BattleType type = request.getType();
		switch (type) {
		case ONE_PLAYER_TO_AI:
			battleService.enterPVE(actorId, request.getCardGroupId());
			break;
		case ONE_PLAYER_TO_ONE_PLAYER:
			battleService.enterPVP(actorId, request.getCardGroupId());
			break;
		case MUTI_PLAYER_TO_AI:
			battleService.enterMutiPVE(actorId, request.getCardGroupId());
			break;

		default:
			break;
		}
		
		return Result.valueOf();
		
	}
	@Cmd(id = BattleCmd.Battle.BATTLE_USE_CARD_VALUE)
	public Result showCard(ChannelHandlerContext context,byte[] bytes, Response response) throws Exception{
		BattleProto.UseCardsRequest request = BattleProto.UseCardsRequest.parseFrom(bytes);
		
		UseCardData sData = request.getData();
		com.come4loves.game.module.fight.model.ShowCardData data = new com.come4loves.game.module.fight.model.ShowCardData(sData.getId(), sData.getTargetIdList());
		long actorId = playerContext.getActorId(context);
		Result result = battleService.useCard(actorId, data);
		if (result.isFail()) {
			return Result.valueOf(result.statusCode);
		}
//		BattleProto.UseCardsResponse.Builder showCardsResponseBuilder = BattleProto.UseCardsResponse.newBuilder();
//		showCardsResponseBuilder.setId(data.getCardId());
//		showCardsResponseBuilder.addAllActs(result.item.toGeneralAct());
		return Result.valueOf();
	}
	@Cmd(id = BattleCmd.Battle.BATTLE_END_VALUE)
	public Result roundEnd(ChannelHandlerContext context,byte[] bytes, Response response) throws Exception{
		
		long actorId = playerContext.getActorId(context);
		return battleService.roundEnd(actorId);
		
	}
	@Cmd(id = BattleCmd.Battle.BATTLE_CHANGE_CARD_VALUE)
	public Result changeCard(ChannelHandlerContext context,byte[] bytes, Response response) throws Exception{
		
		long actorId = playerContext.getActorId(context);
		BattleProto.ChangeCardRequest request = BattleProto.ChangeCardRequest.parseFrom(bytes);
		return battleService.changeCard(actorId, request.getIdList());
		
	}
	
	

}
